package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    public void register(User user) {

        //检查邮箱是否已注册
        User temp=userMapper.selectByEmailAndPassword(user.getEmail(),null);
        if(temp==null) {

            //设置默认的头像和背景图
            user.setHeadImgUrl("/img/user/head.jpg");
            user.setCoverImgUrl("/img/user/bg.jpeg");
            userMapper.insert(user);
        }else {
            throw  new RuntimeException("该邮箱已被注册!");
        }
    }

    public User login(String email,String password) {
        //根据邮箱和密码查询用户
        User temp = userMapper.selectByEmailAndPassword(email, password);
            if (temp==null){
                throw  new RuntimeException("邮箱或密码错误!!");
            }
        return temp;
    }

    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List data = userMapper.queryForList(qo);
        return new PageInfo(data);
    }

    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }
}
